home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15398 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  84 lines

  1. Path: noc.netcom.net!news
  2. From: Tarang Deshpande <tarang@willows.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pascal -> C conversion
  5. Date: Wed, 17 Apr 1996 18:15:34 -0700
  6. Organization: NETCOM Network Operations
  7. Message-ID: <317597B6.38BC@willows.com>
  8. References: <4l3pb3$skd@news.bu.edu>
  9. NNTP-Posting-Host: daffy.willows.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
  14.  
  15. J. Ram wrote:
  16. > Hello netters:
  17. >         I have a simple 23 line pascal program I wrote 5 years ago that
  18. > I'd like to CONVERT to C. Can someone help since I'm not well
  19. > versed with the C syntax. All that needs to be done is change of syntax
  20. > without any change in programming style or approach to the problem.
  21. > This pascal program uses a simple circular linked lists to solve the "Josephus
  22. > Problem".
  23. > Start of program
  24. > ----------------------------------------------------------------------------
  25. > August 25, 1991
  26. > The program is a short program that solves the "Josephus Problem"
  27. > For the Josephus problem, we can imagine that N people have decided to commit
  28. > mass suicide by arranging themselves in circle and killing the M th person
  29. > around the circle, closing ranks as each person drops out of the circle.
  30. > The problem is to find out which person is the last to die or more importantly
  31. > to find the order in which the people were executed.
  32. > Example
  33. >        for N=9 and M=5, the order of execution is 5,1,7,4,3,6,9,2,8
  34. > /* Note that the print out will not produce ',' between numbers since it
  35.     does exactly what the code above does.  If you actually want the
  36.     ',' change the statement:
  37.                 printf ( "%d", (t->next)->key );
  38.     to:
  39.                 printf ( "%d,", (t->next)->key );
  40. */
  41.  
  42. #include <stdio.h>
  43.  
  44. typedef struct _NODE
  45. {
  46.         int             key;
  47.         struct _NODE    *next;
  48. } NODE;
  49.  
  50. int main ( void )
  51. {
  52.         int     i, N, M;
  53.         NODE    *t, *x;
  54.  
  55.         scanf ( "%d %d", &N, &M );
  56.         t = ( NODE* )malloc ( sizeof ( NODE ) );
  57.         t->key = 1;
  58.         x = t;
  59.         for ( i = 2; i <= N; i++ )
  60.         {
  61.                 t->next = ( NODE* )malloc ( sizeof ( NODE ) );
  62.                 t       = t->next;
  63.                 t->key  = i;
  64.         }
  65.         t->next = x;
  66.         while ( t != t->next )
  67.         {
  68.                 for ( i = 1; i < M; i++ )
  69.                         t = t->next;
  70.                 printf ( "%d", (t->next)->key );
  71.                 x = t->next;
  72.                 t->next = (t->next)->next;
  73.                 free ( x );
  74.         }
  75.         printf ( "%d\n", t->key );
  76. }
  77.